﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.DynamicData;
using System.Collections.Specialized;
using System.Collections;
using System.Drawing.Design;
using System.Web;
using System.Security.Permissions;
using System.Drawing;

namespace Catalyst.Web.DynamicData
{
    /// <summary>
    /// The DynamicFilterForm is the container for the DynamicFilterControls.  The DynamicFilterForm will add Parameters to the 
    /// WherePredicateParameters property of a DataSource which implements IPredicateDynamicDataSource. 
    /// </summary>
    [DefaultProperty("DataSource")]
    [ParseChildren(true)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [Designer("Catalyst.Web.DynamicData.Design.DynamicFilterFormDesigner, Catalyst.Web.DynamicData.Design")]
    [ToolboxData("<asp:DynamicFilterForm runat=\"server\"><FilterTemplate><div>Search</div><div><asp:LinkButton ID=\"SearchButton\" runat=\"server\" CommandName=\"Search\" Text=\"Search\" /><asp:LinkButton ID=\"ClearButton\" runat=\"server\" CommandName=\"Clear\" Text=\"Clear\" /> </div></FilterTemplate></asp:DynamicFilterForm>")]
    [ToolboxBitmap(typeof(DynamicFilterForm))]
    public class DynamicFilterForm : DynamicFilterContainerBase
    {
        
        private TemplateOwner _templateOwner;

        [Browsable(false)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DefaultValue(typeof(FilterTemplate), "")]
        [Description("Filter Template")]
        [TemplateContainer(typeof(DynamicFilterForm))]
        public ITemplate FilterTemplate { get; set; }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public TemplateOwner TemplateOwner
        {
            get { return _templateOwner; }
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            _templateOwner = new TemplateOwner();

            ITemplate temp = FilterTemplate;
            if (temp == null)
                temp = new FilterTemplate();

            temp.InstantiateIn(_templateOwner);
            this.Controls.Add(_templateOwner);
            base.CreateChildControls();
        }

    }

    [ToolboxItem(false)]
    public class TemplateOwner : WebControl, INamingContainer
    {
        
    }

    sealed class FilterTemplate : ITemplate
    {
        void ITemplate.InstantiateIn(Control owner)
        {

        }
    }
    
}
